from ctypes import *
import os
import time
import struct
import sys
from pathlib import Path


module_path = Path("/usr/local/RoboLib/PythonScrips")
sys.path.append(str(module_path))
from RoboPy import *

filepath=("./CoreSources/share_mem_135.ini")

sbf = get_sharemem_fromfile(filepath)
_lib = cdll.LoadLibrary("./build/libtest_share_mem.so")

_lib.sharemem_client_build.argtypes = [POINTER(share_buffer),c_int,c_int,c_int]
psbf=POINTER(share_buffer)(sbf)
_lib.sharemem_client_build(psbf,sbf.data_size,sbf.semid_cnt,sbf.key)

_lib.get_python_pointer.argtypes = [POINTER(share_buffer)]
_lib.get_python_pointer.restype = c_void_p
python_data=cast(_lib.get_python_pointer(psbf),POINTER(c_double))

i=0
while 1:
    i+=0.1
    _lib.locksem(sbf.semid, 1) #锁定USR_SEMID_RT_STATUS，锁定特定的内存进行读取、更改
    python_data[0]=i
    python_data[1]=i*2
    python_data[2]=i*3
    python_data[3]=-i*4
    _lib.unlocksem(sbf.semid, 1)
    print(python_data[0],python_data[1],python_data[2],python_data[3])
    time.sleep(0.1)


